package project.config;

import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
import org.springframework.transaction.annotation.EnableTransactionManagement;



import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

import project.dao.RoomDAO;
import project.dao.RoomDAOImpl;
import project.model.Pricing;
import project.model.Room;
import project.model.RoomCategory;

@Configuration
@EnableTransactionManagement
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter{
	
	SessionFactory sessionFactory;
	
	@Override
	public void configureDefaultServletHandling(
			DefaultServletHandlerConfigurer configurer) {
		configurer.enable();
	}
	
	@Bean
	public HibernateTemplate getHibernateTemplate(){
		return new HibernateTemplate(getSessionFactory());
	}
	
	//Remember to register the Dependent Injection object
	@Bean
	public RoomDAO roomDAO(){
		return new RoomDAOImpl();
	}
	
	@Bean
	public SessionFactory getSessionFactory(){
		sessionFactory = new LocalSessionFactoryBuilder(getDataSource()).addAnnotatedClasses(Room.class,RoomCategory.class,Pricing.class).buildSessionFactory();
		return sessionFactory;
	}
	public void setSessionFactory(SessionFactory sessionFactory){
		this.sessionFactory = sessionFactory;
	}
	@Bean
	public javax.sql.DataSource getDataSource(){
		org.apache.commons.dbcp.BasicDataSource basicDataSource = new org.apache.commons.dbcp.BasicDataSource();
		basicDataSource.setDriverClassName("com.mysql.jdbc.Driver");
		basicDataSource.setUrl("jdbc:mysql://localhost:3306/lipotes");
		basicDataSource.setUsername("admin");
		basicDataSource.setPassword("admin");
		return basicDataSource;
	}
	@Bean
	public HibernateTransactionManager getHibTranManager(){
		return new HibernateTransactionManager(getSessionFactory());
	}

	/**
	 *  Every view name returned from a handler will be translated to a JSP resource (for example: 
	 *  "myView" -> "/WEB-INF/jsp/myView.jsp"), using this view class to enable explicit JSTL support. 
	 * @return an instance of InternalResourceViewResolver
	 */
	@Bean
	public InternalResourceViewResolver viewResolver(){
		InternalResourceViewResolver resolver = new InternalResourceViewResolver();
		resolver.setViewClass(JstlView.class);
		resolver.setPrefix("/WEB-INF/views/");
		resolver.setSuffix(".jsp");
		return resolver;
	}
	
	//Remember to compile file-upload package in build.gradle
	@Bean
	public CommonsMultipartResolver multipartResolver(){
		CommonsMultipartResolver comMultipartResolver = new CommonsMultipartResolver();
		comMultipartResolver.setDefaultEncoding("UTF-8");
		comMultipartResolver.setMaxUploadSize(10485760000l);
		comMultipartResolver.setMaxInMemorySize(10960);
		return comMultipartResolver;
	}
	
}
